package design;

import java.util.Stack;

/**
 * @description:
 * 用栈实现队列
 * @author: mayunfei
 * @date: 2025/1/6 6:43
 */
public class MyQueue {

    private Stack<Integer> inStack;
    private Stack<Integer> outStack;

    public MyQueue() {
        inStack = new Stack<>();
        outStack = new Stack<>();
    }

    public void push(int x) {
        inStack.push(x);
    }

    public int pop() {
        if (outStack.isEmpty()) move();
        return outStack.pop();
    }

    public int peek() {
        if (outStack.isEmpty()) move();
        return outStack.peek();
    }

    public boolean empty() {
        return inStack.isEmpty() && outStack.isEmpty();
    }

    private void move() {
        while (!inStack.isEmpty()) {
            outStack.push(inStack.pop());
        }
    }
}
